package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.change.dispatch.mapper.ChangeOrderDispatchWorkerMapper;
import com.koron.order.change.util.ThreadLocalOrderChange;
import com.koron.order.common.bean.base.OrderType;
import com.koron.order.common.bean.po.OrderDispatchWorker;
import com.koron.order.common.mapper.OrderDispatchWorkerMapper;
import com.koron.order.common.service.OrderDispatchWorkerService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (OrderDispatchWorker)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-03 16:09:54
 */
@Service("orderDispatchWorkerService")
public class OrderDispatchWorkerServiceImpl implements OrderDispatchWorkerService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderDispatchWorker orderDispatchWorker) {

        int i = getOrderDispatchWorkerMapper(factory).insert(orderDispatchWorker);
        return i;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderDispatchWorker> orderDispatchWorkerList) {

        if (CollectionUtils.isEmpty(orderDispatchWorkerList)) {
            return 0;
        }

        OrderDispatchWorkerMapper mapper = getOrderDispatchWorkerMapper(factory);
        //mapper.deleteByDispatchIdOrderId(orderDispatchWorkerList.get(0).getDispatchId(), orderDispatchWorkerList.get(0).getOrderId());
        orderDispatchWorkerList.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertBatch(orderDispatchWorkerList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderDispatchWorker orderDispatchWorker) {
        int i = getOrderDispatchWorkerMapper(factory).update(orderDispatchWorker);
        return i;
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        int i = getOrderDispatchWorkerMapper(factory).deleteById(id);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        int i = getOrderDispatchWorkerMapper(factory).deleteByOrderId(orderId);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByDispatchId")
    public int deleteByDispatchId(SessionFactory factory, String dispatchId) {
        int i = getOrderDispatchWorkerMapper(factory).deleteByDispatchId(dispatchId);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByDispatchIdOrderId")
    public int deleteByDispatchIdOrderId(SessionFactory factory, String dispatchId, String orderId) {
        int i = getOrderDispatchWorkerMapper(factory).deleteByDispatchIdOrderId(dispatchId, orderId);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByTeamId")
    public int deleteByTeamId(SessionFactory factory, String teamId) {
        int i = getOrderDispatchWorkerMapper(factory).deleteByTeamId(teamId);
        return i;
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderDispatchWorker queryById(SessionFactory factory, String id) {

        OrderDispatchWorker orderDispatchWorker = getOrderDispatchWorkerMapper(factory).queryById(id);

        return orderDispatchWorker;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<OrderDispatchWorker> queryByOrderId(SessionFactory factory, String orderId) {

        List<OrderDispatchWorker> orderDispatchWorkerList = getOrderDispatchWorkerMapper(factory).queryByOrderId(orderId);

        return orderDispatchWorkerList;
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, OrderDispatchWorker orderDispatchWorker, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());

        List<OrderDispatchWorker> list = getOrderDispatchWorkerMapper(factory).queryList(orderDispatchWorker);

        return new PageInfo(list);
    }

    OrderDispatchWorkerMapper getOrderDispatchWorkerMapper(SessionFactory factory) {

        if (OrderType.CHANGE.equals(ThreadLocalOrderChange.get())) {
            return factory.getMapper(ChangeOrderDispatchWorkerMapper.class);
        }
        return factory.getMapper(OrderDispatchWorkerMapper.class);
    }

}